package com.ruoyi.oar.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.oar.mapper.OarRoomsMapper;
import com.ruoyi.oar.domain.OarRooms;
import com.ruoyi.oar.domain.dto.OarRoomsDTO;
import com.ruoyi.oar.domain.query.OarRoomsQuery;
import com.ruoyi.oar.service.OarRoomsService;
import java.util.stream.Collectors;
import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.page.PageUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import java.util.Optional;

/**
 * 房间Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-02-17
 */
@Service
public class OarRoomsServiceImpl implements OarRoomsService
{
    @Autowired
    private OarRoomsMapper oarRoomsMapper;

    /**
     * 查询房间
     * 
     * @param id 房间主键
     * @return 房间
     */
    @Override
    public Optional<OarRoomsDTO> selectOarRoomsById(String id) {
        return oarRoomsMapper.selectOarRoomsById(id).map(oarRooms -> toDto(oarRooms,true));
    }

    /**
     * 查询房间列表
     * 
     * @param oarRoomsQuery 房间
     * @return 房间
     */
    @Override
    public List<OarRoomsDTO> selectOarRoomsList(OarRoomsQuery oarRoomsQuery) {
        return oarRoomsMapper.selectOarRoomsList(oarRoomsQuery).stream().map(this::toDto).collect(Collectors.toList());
    }

    /**
     * 分页查询房间列表
     * @param oarRoomsQuery 房间
     * @return
     */
    @Override
    public TableDataInfo<OarRoomsDTO> findAll(OarRoomsQuery oarRoomsQuery) {
        List<OarRooms> list = oarRoomsMapper.selectOarRoomsList(oarRoomsQuery);
        List<OarRoomsDTO> dtoList = list.stream().map(this::toDto).collect(Collectors.toList());
        return PageUtil.getDataTable(dtoList,new PageInfo(list).getTotal());
    }
    /**
     * 新增房间
     * 
     * @param oarRoomsDTO 房间
     * @return 结果
     */
    @Override
    public int insertOarRooms(OarRoomsDTO oarRoomsDTO) {
        checkOarRooms(oarRoomsDTO);
        return oarRoomsMapper.insertOarRooms(toEntity(oarRoomsDTO));
    }

    /**
     * 修改房间
     * 
     * @param oarRoomsDTO 房间
     * @return 结果
     */
    @Override
    public int updateOarRooms(OarRoomsDTO oarRoomsDTO) {
        checkOarRooms(oarRoomsDTO);
        return oarRoomsMapper.updateOarRooms(toEntity(oarRoomsDTO));
    }

    /**
     * 批量删除房间
     * 
     * @param ids 需要删除的房间主键
     * @return 结果
     */
    @Override
    public int deleteOarRoomsByIds(String[] ids) {
        return oarRoomsMapper.deleteOarRoomsByIds(ids);
    }

    /**
     * 删除房间信息
     * 
     * @param id 房间主键
     * @return 结果
     */
    @Override
    public int deleteOarRoomsById(String id)
    {
        return oarRoomsMapper.deleteOarRoomsById(id);
    }

    /**
     * 根据房屋信息删除房间信息
     * @param houseId
     * @return
     */
    @Override
    public int deleteOarRoomsByHouseId(String houseId)
    {
        return oarRoomsMapper.deleteOarRoomsByHouseId(houseId);
    }

    /**
     * 根据房屋id查询房间
     * @param houseId
     * @return
     */
    @Override
    public List<OarRoomsDTO> selectOarRoomsByHouseId(String houseId) {
        return oarRoomsMapper.selectOarRoomsByHouseId(houseId).stream().map(this::toDto).collect(Collectors.toList());
    }



    /**
    * 校验
    * @param dto
    */
    private void checkOarRooms(OarRoomsDTO dto) {

    }

    private OarRooms toEntity(OarRoomsDTO dto) {
        return BeanUtil.toBean(dto, OarRooms.class);
    }

    private OarRoomsDTO toDto(OarRooms entity) {
        return toDto(entity, false);
    }
    /**
     * 转DTO
     *
     * @param entity
     * @param withDetail 是否查询其他信息
     * @return
     */
    private OarRoomsDTO toDto(OarRooms entity, Boolean withDetail) {
            OarRoomsDTO dto =  BeanUtil.toBean(entity, OarRoomsDTO.class);
        if (withDetail) {

        }
        return dto;
    }
}
